package com.hdl.servlet.cart;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdl.entity.CartItem;
import com.hdl.entity.HdlUser;
import com.hdl.entity.MallCart;
import com.hdl.entity.MallProduct;
import com.hdl.service.MallProductService;
import com.hdl.service.impl.MallProductServiceImpl;

/**
 *     添加商品到购物车
 * @author Administrator
 *
 */
@WebServlet("/addProduct.let")
public class AddToCartServlet extends HttpServlet{
	
	private MallProductService service = new MallProductServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//判断是否登陆
		HttpSession session = req.getSession();
		HdlUser user = (HdlUser) session.getAttribute("user");
		resp.setContentType("application;json;charset=utf-8");
		
		//创建核心对象
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map = new HashMap<String,Object>();
		if(user == null) {//没有登录
			System.out.println("没有登录");
			map.put("code","1");
			mapper.writeValue(resp.getOutputStream(), map);
			return;
		}
		
		System.out.println("登陆过");
		
		//登陆过
		map.put("code",2);
		//获取商品id参数
		String productId = req.getParameter("mallProductId");
		
		//获取商品数量
		String _num = req.getParameter("num");
		int num = Integer.valueOf(_num);
		
		//调用商品业务逻辑层方法查询商品
		MallProduct product = service.SelectProductById(productId);
//		System.out.println(product);
		//创建购物项对象
		CartItem cartItem = new CartItem();
		cartItem.setProduct(product);//商品
		cartItem.setNum(num);//商品数量
		cartItem.setTotal(cartItem.getTotal());//总计
//		System.out.println("更新前"+cartItem.getProduct().getMallProductName()+":"+cartItem.getTotal());
		
		//看session中是否有购物车对象
		MallCart cart = (MallCart) session.getAttribute("cart");
		if(cart == null) {//没有
			//创建购物车对象
			cart = new MallCart();
		}//有
		//添加商品进购物车  给购物车属性赋值
		cart.addProduct(product.getMallProductId(), cartItem);//添加商品
		cart.setHdlUserId(user.getHdlUserId());//某个用户的购物车
		cart.setMallCartTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));//购物车的创建时间
		int mallCartTotal = 0;
		Set<String> keySet = cart.getProductMap().keySet();
		for (String key : keySet) {
			CartItem Item = cart.getProductMap().get(key);
			mallCartTotal += Item.getProduct().getMallProductPrice1() * Item.getNum();
		}
		cart.setMallCartTotal(mallCartTotal);//计算总计
//		System.out.println("更新总计前价格"+cart.getMallCartTotal());
		
		//将购物车存入session中
		session.setAttribute("cart", cart);
		
		//写回消息
		mapper.writeValue(resp.getOutputStream(), map);
		
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
